<?php
/**
 * 关注执行
 * @author Ice <iceinto@mallog.com.cn>
 */
class AttentionAction extends BaseAction {
	public function index(){
		$MarketAttentionModel = D('MarketAttention');
		$malls = $MarketAttentionModel->getTopTen();

		$BrandAttentionModel = D('BrandAttention');
		$brands = $BrandAttentionModel->getTopTen();

		$this->assign('malls', $malls);
		$this->assign('brands', $brands);
		$this->display();
	}

	/**
	 * 检查对应关注当前用户是否已经关注
	 * @author Ice <iceinto@mallog.com.cn>
	 */
	public function attCheck(){
		if(!$this->isAjax()){
			exit;
		}
		$status = 0;
		$data = '你登陆了吗？请先登陆管理关注！';
		$type = intval($this->_get('type'));
		$id = intval($this->_get('id'));
		$uid = getLoginUserInfo();
		$att = null;
		if($uid){
			if($type ==1 && $id){
				//执行品牌关注检查
				$att = D('BrandAttention')->findAtt($uid, $id);
			}else if($type ==2 && $id){
				//执行商场关注检查
				$att =  D('MarketAttention')->findAtt($uid, $id);
			}else{
				return $this->ajaxReturn('数据有错误！', null, $status);
			}
		}else{
			return $this->ajaxReturn('请先登录！', null, $status);
		}
		if($att){
			$status = 1;
			return $this->ajaxReturn($att, '已经关注', $status);
		}else{
			return $this->ajaxReturn('未关注！', '未关注', $status);
		}
	}

	/**
	 * 执行删除关注操作
	 * @author Ice <iceinto@mallog.com.cn>
	 */
	public function cancel(){
		if(!$this->isAjax()){
			exit;
		}
		$status = 0;
		$data = '你登陆了吗？请先登陆管理关注！';
		$type = intval($this->_get('type'));
		$id = intval($this->_get('id'));
		$uid = getLoginUserInfo();
		if($uid){
			if($type ==1 && $id){
				//执行删除品牌关注
				$data = $this->brandDeleteAtt($uid, $id);
				$status = intval($data);
			}else if($type ==2 && $id){
				//执行删除商场关注
				$data = $this->mallDeleteAtt($uid, $id);
				$status = intval($data);
			}else{
				$data = '你提供的关注删除信息不正确！';
			}
		}
		$this->ajaxReturn($data, '关注删除', $status);
	}

	/**
	 * 执行添加关注操作
	 * @author Ice <iceinto@mallog.com.cn>
	 */
	public function add(){
		if(!$this->isAjax()){
			exit;
		}
		$status = 0;
		$data = '你登陆了吗？请先登陆再关注！';
		$type = intval($this->_get('type'));
		$id = intval($this->_get('id'));
		$mid = intval($this->_get('mid'));
		$fid = intval($this->_get('fid'));
		$uid = getLoginUserInfo();
		if($uid){
			if($type ==1 && $id){
				//执行添加品牌关注
				$data = $this->brandAddAttCheck($uid, $id, $mid, $fid);
				$status = intval($data) ? 1 : 0;
			}else if($type ==2 && $id){
				//执行添加商场关注
				$data = $this->mallAddAttCheck($uid, $id);
				$status = intval($data) ? 1 : 0;
			}else{
				$data = '你提供的关注信息不正确！';
			}
		}else{
			$status = 3;
		}
		$this->ajaxReturn($data, 0, $status);
	}

	/**
	 * 进行品牌关注添加操作
	 * @param int $uid 用户
	 * @param int $id 标识
	 * @param int $mid 商场
	 * @param int $fid 楼层
	 * @return string
	 * @author Ice <iceinto@mallog.com.cn>
	 */
	public function brandAddAttCheck($uid, $bid, $mid, $fid){
		$result = null;
		$BrandAttentionModel = D('BrandAttention');
		$BrandModel = D('Brand');
		$brand = $BrandModel->getById($bid);
		if($brand){
			$result = $BrandAttentionModel->addAtt($uid, $bid, $mid, $fid);
			if($result){
				$result= 1;
			}else{
				//重复查询
				$brandAtt = $BrandAttentionModel->findAtt($uid, $bid);
				if($brandAtt){
					$result = '你已经关注此品牌！';
				}else{
					$result = '添加关注失败！';
				}
			}
		}else{
			$result = '品牌信息提供有错误！';
		}
		return $result;
	}

	/**
	 * 删除品牌关注
	 * @param int $uid 用户
	 * @param int $bid 品牌
	 * @author Ice <iceinto@mallog.com.cn>
	 */
	public function brandDeleteAtt($uid, $bid){
		$result = null;
		$BrandAttentionModel = D('BrandAttention');
		$brandAtt = $BrandAttentionModel->findAtt($uid, $bid);
		if($brandAtt){
			$result = $BrandAttentionModel->deleteAtt($uid, $bid);
			if($result){
				$result = 1;
			}else{
				$result = '关注品牌删除失败！';
			}
		}else{
			$result = '你对应的关注品牌不存在！';
		}
		return $result;
	}

	/**
	 * 进行商场关注添加操作
	 * @param int $uid
	 * @param int $mid
	 * @return string
	 * @author Ice <iceinto@mallog.com.cn>
	 */
	public function mallAddAttCheck($uid, $mid){
		$result = null;
		$MarketAttentionModel = D('MarketAttention');
		$MarketModel = D('Market');
		$mall = $MarketModel->getById($mid);
		if($mall){
			$result = $MarketAttentionModel->addAtt($uid, $mid);
			if($result){
				$result = 1;
			}else{
				//重复查询
				$mallAtt = $MarketAttentionModel->findAtt($uid, $mid);
				if($mallAtt){
					$result = '你已经关注此商场！';
				}else{
					$result = '添加关注失败！';
				}
			}
		}else{
			$result = '商场信息提供有错误！';
		}
		return $result;
	}

	/**
	 * 进行关注商场删除
	 * @param int $uid 用户
	 * @param int $mid 商场
	 * @author Ice <iceinto@mallog.com.cn>
	 */
	public function mallDeleteAtt($uid, $mid){
		$result = null;
		$MarketAttentionModel = D('MarketAttention');
		$mallAtt = $MarketAttentionModel->findAtt($uid, $mid);
		if($mallAtt){
			$result = $MarketAttentionModel->deleteAtt($uid, $mid);
			if($result){
				$result = 1;
			}else{
				$result = '关注商场删除失败！';
			}
		}else{
			$result = '你对应的关注商场不存在！';
		}
		return $result;
	}
}
